package com.iss.apartmentmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iss.apartmentmanage.entity.RoomManage;
import com.iss.apartmentmanage.mapper.HotelAreaMapper;
import com.iss.apartmentmanage.mapper.RoomManagerMapper;
import com.iss.apartmentmanage.model.BaseModel;
import com.iss.apartmentmanage.service.RoomService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RoomManagerServiceImpl implements RoomService {
    @Autowired
    RoomManagerMapper roomManagerMapper;
    @Autowired
    HotelAreaMapper hotelAreaMapper;

    /**
     * 查询全部房间
     * @param pageNum
     * @param pageSize
     * @param roomManage
     * @param baseModel
     * @return
     */
    @Override
    public IPage<RoomManage> selectAllRoom(int pageNum, int pageSize, RoomManage roomManage, BaseModel baseModel) {
        Page<RoomManage> page = new Page<>(pageNum,pageSize);
        IPage<RoomManage> iPage = null;

        QueryWrapper<RoomManage> wrapper= new QueryWrapper();
        if(StringUtils.isNotEmpty(baseModel.getRoomName())) {
             wrapper.eq("room_name", baseModel.getRoomName());

        }
        if (roomManage.getAreaId()!=null){
            wrapper.eq("area_id",roomManage.getAreaId());

        }
        if(baseModel.getPriceFrom()!=null&& baseModel.getPriceTo()!=null){
            wrapper.between("room_price", baseModel.getPriceFrom(), baseModel.getPriceTo());

        }
        iPage =roomManagerMapper.selectPage(page,wrapper);
        return iPage;
    }

    /**
     *  添加房间
     * @param roomManage
     * @return
     */

    @Override
    public Integer insertRoom(RoomManage roomManage) {
        int room=0;
        if(roomManage!=null){
            try {
                room = roomManagerMapper.insert(roomManage);
            }catch (Exception e){
                throw new RuntimeException("插入失败");
            }
        }
        return room;
    }
    //查询所有空房间
    @Override
    public List<RoomManage> selectAllEmptyRoom() {
        List<RoomManage> roomManages = roomManagerMapper.selectList(null);
        return roomManages;
    }
    //查询所有入住房间
    @Override
    public List<RoomManage> selectAllCheckinRoom() {
        return null;
    }
    //更新
    @Override
    public Integer updateRoomByRoomId(RoomManage roomManage) {
        return roomManagerMapper.updateById(roomManage);
    }
    //批量更新
    @Override
    public Integer updateIsUsedByRoomIds(String[] id,int isUser) {
        int count=0;
        try {
            count =roomManagerMapper.updateIsUsedByRoomIds(id,isUser);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return count;

    }
    //单个删除
    @Override
    public Integer deleteRoomByid(String id) {
        return roomManagerMapper.deleteById(id);
    }
    //批量删除
    @Override
    public Integer deleteRoomByBatchIds(String[] arr) {
         int count=roomManagerMapper.deleteBatchIds(Arrays.asList(arr));
        return count;
    }

    @Override
    public Integer updateRoomPicture(RoomManage roomManage) {
        int count = 0;
        if(roomManage!=null){
//            QueryWrapper wrapper = new QueryWrapper();
//            wrapper.eq("room_picture",roomManage.getRoomPicture());
             count = roomManagerMapper.updateById(roomManage);

        }
        return count;
    }

    //    //根据区域编号查询
//    @Override
//    public IPage<RoomManage> selectAllRoomByArea(Integer areaName) {
//        Page<RoomManage> page = new Page<>(1,10);
//        QueryWrapper<RoomManage> wrapper = new QueryWrapper<>();
//        wrapper.eq("area_code",areaName);
//        IPage<RoomManage> iPage = roomManagerMapper.selectPage(page,wrapper);
//        return iPage;
//    }
//    //根据房间名查询
//    @Override
//    public IPage<RoomManage> selectAllRoomByRoomName(String roomName) {
//        Page<RoomManage> page = new Page<>(1,10);
//        QueryWrapper<RoomManage> wrapper = new QueryWrapper<>();
//        wrapper.eq("roomName",roomName);
//        IPage<RoomManage> roomManageIPage = roomManagerMapper.selectPage(page, wrapper);
//        return roomManageIPage;
//    }
//    //根据价格区间查询
//    @Override
//    public IPage<RoomManage> selectAllRoomByPrice(Double priceFrom,Double priceTo) {
//        Page<RoomManage> page = new Page<>(1,10);
//        QueryWrapper<RoomManage> wrapper = new QueryWrapper<>();
//        wrapper.between("room_price",priceFrom,priceTo);
//        IPage<RoomManage> roomManageIPage = roomManagerMapper.selectPage(page,wrapper);
//        return roomManageIPage;
//    }
}
